package uk.org.landeg.euler.problems;

import uk.org.landeg.euler.problems.framework.AbstractProblem;
import uk.org.landeg.euler.problems.framework.ProblemLauncher;
import uk.org.landeg.euler.problems.lib.Fraction;

public class Problem033 extends AbstractProblem{
	public static void main(String[] args) {
		ProblemLauncher launcher = new ProblemLauncher(new Problem033());
		launcher.solve();		
	}

	@Override
	public String getDescription() {
		return "Discover all the fractions with an unorthodox cancelling method.";
	}

	@Override
	public Integer getProblemNumber() {
		return 33;
	}

	@Override
	public Object solve() {
		notes.add("Candidates are of form ab/cd");
		notes.add("Satisfaction criteria is 10a+b/10c+d = a/d or b/c");
		Fraction f1,f2,product = new Fraction(1,1);
		int top, bot;
		for (int a = 1 ; a < 10 ; a++) {
			for (int b = 0 ; b < 10 ; b++) {
				for (int c = 1 ; c < 10 ; c++) {
					for (int d = 0 ; d < 10 ; d++) {
						top = a * 10 + b;
						bot = c * 10 + d;
						if (top > bot) {
							f1 = new Fraction(top,bot);
							f2 = null;
							if (a == d) {
								f2 = new Fraction(b,c);
							} else if (b == c) {
								f2 = new Fraction(a,d);
							}
							if (f2 != null) {
								if (Fraction.reduce(f1).equals(Fraction.reduce(f2))) {
									System.out.println(f1);
									product.setN(product.getN() * top);
									product.setD(product.getD() * bot);
								}
							}
						}
					}
				}
			}
		}
		return Fraction.reduce(product);
	}
}
